<?php

namespace app\admin\model;

use app\common\model\BaseModel;


class Record extends BaseModel
{


    // 表名
    protected $name = 'record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'signtime_text',
        'createtime_text',
        'user1_sign_status_text',
        'user1_sign_time_text',
        'user2_sign_status_text',
        'user2_sign_time_text',
        'is_success_text'
    ];


    public function getList($where, $size = 12, $field = '*', $order = 'id desc')
    {
        $list = $this->where(function ($query) use ($where) {

            if (isset($where['user_id']) && $where['user_id']) {
                $query->where('user_id', $where['user_id']);
            }
            if (isset($where['keywords']) && $where['keywords']) {
                $query->where('username1|username2|title|mobile1|mobile2', 'like', '%' . $where['keywords'] . '%');
            }

            if (isset($where['status']) && $where['status']>-1) {
                $query->where('status', $where['status']);
            }

            if (isset($where['createtime']) && $where['createtime']) {
                list($start, $end) = explode('/', $where['createtime']);
                $query->where('createtime', '>=', strtotime($start));
                $query->where('createtime', '<', strtotime($end) + 86400);
            }

            if (isset($where['signtime']) && $where['signtime']) {
                list($start, $end) = explode('/', $where['signtime']);
                $query->where('signtime', '>=', strtotime($start));
                $query->where('signtime', '<', strtotime($end) + 86400);
            }
            if (isset($where['sign_type']) && $where['sign_type']) {
                if ($where['sign_type'] == 1) {
                    $query->where('user1_sign_status', 'in', [0, 1]);
                    $query->where('status', 1);
                }
                if ($where['sign_type'] == 2) {
                    $query->where('user2_sign_status', 'in', [0, 1]);
                    $query->where('status', 1);
                }
            }

        })
            ->with([])
            ->field($field)
            ->order($order)
            ->paginate($size);
        //$list->each(function ($item) {
        //});
        return $list->toArray();

    }


    public function getStatusList()
    {
        return [ '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '5' => __('Status 5')];
    }

    public function getUser1SignStatusList()
    {
        return ['0 ' => __('User1_sign_status 0 '), '1' => __('User1_sign_status 1'), '

2' => __('User1_sign_status 

2')];
    }

    public function getUser2SignStatusList()
    {
        return ['0 ' => __('User2_sign_status 0 '), '1' => __('User2_sign_status 1'), '

2' => __('User2_sign_status 

2')];
    }

    public function getIsSuccessList()
    {
        return ['0' => __('Is_success 0'), '1' => __('Is_success 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSigntimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['signtime']) ? $data['signtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUser1SignStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user1_sign_status']) ? $data['user1_sign_status'] : '');
        $list = $this->getUser1SignStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUser1SignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user1_sign_time']) ? $data['user1_sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUser2SignStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user2_sign_status']) ? $data['user2_sign_status'] : '');
        $list = $this->getUser2SignStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUser2SignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user2_sign_time']) ? $data['user2_sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsSuccessTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_success']) ? $data['is_success'] : '');
        $list = $this->getIsSuccessList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setSigntimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUser1SignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUser2SignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
